<?php

//
// +---------------------------------------------------------+
// | 开发组
// +---------------------------------------------------------+
// | Copyrigh  (c)  2020 The PHP Group
// +---------------------------------------------------------+
// | 商户 代理商
// +---------------------------------------------------------+
// | zxc
// | php后端开发
// |
// +———————————————————+
//

namespace App\Models\Admin;

use App\Tools\Financial;
use App\Tools\Prstatic;

namespace App\Models\Admin;


use Illuminate\Database\Eloquent\Model;

class StockOrderGoods extends Model
{

    protected $table = 'stock_order_goods';
    protected $primaryKey = 'id';
    protected $dateFormat = 'U'; // 时间戳

    public static function getListByOid($order_id)
    {
        $list = self::from('stock_order_goods as sg')
            ->select(['sg.*', 'c.cate_name', 'g.goods_specification', 'g.goods_unit', 'g.goods_price as goods_price_yu', 'g.goods_img', 'g.goods_sn', 'g.goods_name'])
            ->leftJoin('goods as g', 'g.id', '=', 'sg.goods_id')
            ->leftJoin('goods_category as c', 'c.id', '=', 'g.cate_id')
            ->where(['sg.order_id' => $order_id])
            ->get();
        return $list;
    }

    /**
     * 获取出库列表
     * @param $param
     * @param int $limit
     * @return mixed
     */
    public static function getOutboundList($param, $limit = 15)
    {
        $where[] = ['so.type', '=', 1];
        if (isset($param['store_id']) && (int)$param['store_id'] > 0) {
            $where[] = ['sog.store_id', '=', (int)$param['store_id']];
        }
        if (isset($param['start']) && !empty($param['start'])) {
            $where[] = ['sog.created_at', '>', strtotime($param['start'])];
        }
        if (isset($param['end']) && !empty($param['end'])) {
            $where[] = ['sog.created_at', '<', strtotime($param['end'])];
        }

        $sql = self::from('stock_order_goods as sog')->where($where);
        if (isset($param['goods_ids']) && !empty($param['goods_ids'])) {
            $goodsIds = explode(',', $param['goods_ids']);
            $sql->whereIn('g.id', $goodsIds);
        }
        $list = $sql->select(['sog.*', 'so.order_sn', 'g.goods_sn', 'g.goods_name', 'g.goods_price as old_price', 's.store_name'])
            ->join('stock_order as so', 'so.id', '=', 'sog.order_id')
            ->leftJoin('goods as g', 'g.id', '=', 'sog.goods_id')
            ->leftJoin('store as s', 's.id', '=', 'sog.store_id')
            ->orderBy('sog.id', 'desc')
            ->paginate($limit);

        return $list;
    }

    /**
     * 获取出库列表
     * @param $param
     * @param int $limit
     * @return mixed
     */
    public static function getListByWhere($param, $limit = 15)
    {
        $where = array();
        if (isset($param['start']) && !empty($param['start'])) {
            $where[] = ['sog.created_at', '>', strtotime($param['start'])];
        }
        if (isset($param['end']) && !empty($param['end'])) {
            $where[] = ['sog.created_at', '<', strtotime($param['end'])];
        }
        if (isset($param['store_id']) && !empty($param['store_id'])) {
            $where[] = ['sog.store_id', '=', $param['store_id']];
        }

        $sql = self::from('stock_order_goods as sog')->where($where);
        if (isset($param['goods_ids']) && !empty($param['goods_ids'])) {
            $goodsIds = explode(',', $param['goods_ids']);
            $sql->whereIn('g.id', $goodsIds);
        }
        $list = $sql->select(['sog.*', 'so.order_sn', 'so.type', 'g.goods_sn', 'g.goods_name', 'g.goods_specification', 'g.goods_unit', 's.store_name'])
            ->join('stock_order as so', 'so.id', '=', 'sog.order_id')
            ->leftJoin('goods as g', 'g.id', '=', 'sog.goods_id')
            ->leftJoin('store as s', 's.id', '=', 'sog.store_id')
            ->orderBy('sog.id', 'desc')
            ->paginate($limit);

        return $list;
    }
}